package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.service.DubboCartService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Reference(timeout = 3000,check = false)
    private DubboCartService cartService;

    /**
     * 业务需求: 根据userId查询购物车数据
     * url地址: http://www.jt.com/cart/show.html
     * 请求参数: 动态获取userId
     * 返回值结果:  cart.jsp页面
     * 页面取值方式: ${cartList}
     */
    @RequestMapping("/show")
    public String findCartListByUserId(Model model){
        Long userId = 7L;
        List<Cart> cartList = cartService.findCartListByUserId(userId);
        model.addAttribute("cartList",cartList);
        return "cart";
    }

    /**
     * 业务：实现购物车数量的更新
     * url： http：//
     * 参数：itemId/num
     * 返回值：void
     */
    @RequestMapping("/update/num/{itemId}/{num}")
    @ResponseBody
    public void updateCartNum(Cart cart){

        Long userId = 7L;
        cart.setUserId(userId);
        cartService.updateCartNum(cart);
    }

    /**
     * 完成购物车的新增
     * url：
     * 参数:form扁担提交 对象接收
     * 返回值：重定向到购物车列表页面中
     */
    @RequestMapping("/add/{itemId}")
    public String saveCart(Cart cart){
        Long userId = 7L;
        cart.setUserId(userId);
        cartService.saveCart(cart);
        return "redirect:/cart/show.html";
    }

}
